package org.vectrics.recruiter.config;


import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;



/**
 * @author mmoore
 */
public class NotificationsConfig {
	private transient static Logger log = Logger.getLogger(NotificationsConfig.class);
	private boolean notificationsEnabled = false;
	private Map<String, String> config = new HashMap<String, String>();
	
	public NotificationsConfig() {
		super();
	}

	public void loadConfig(Element mainElement) {
		String enabledStr = mainElement.getAttribute("enabled");
		
		this.notificationsEnabled = false;
		if ((enabledStr != null) && (enabledStr.trim().equalsIgnoreCase("true"))) {
			log.info("Event Notifications Enabled");
			this.notificationsEnabled = true;
		} else {
			log.info("Event Notifications Disabled");
		}
		
		if (this.notificationsEnabled) {
			log.info("Loading broadcast configurations...");
			NodeList children = mainElement.getElementsByTagName("notification");
			Element element = null;
			for (int i = 0; i < children.getLength(); i++) {
				log.info("loadModule() - module element found.");
				element = (Element)children.item(i);
				config.put(element.getAttribute("name"), element.getAttribute("value"));
			}			
		}

	}


	/**
	 * @return Returns the notificationsEnabled.
	 */
	public boolean getNotificationsEnabled() {
		return notificationsEnabled;
	}
	
	/**
	 * @param notificationsEnabled The notificationsEnabled to set.
	 */
	public void setNotificationsEnabled(boolean notificationsEnabled) {
		this.notificationsEnabled = notificationsEnabled;
	}

	/**
	 * @return the config
	 */
	public Map<String, String> getConfig() {
		return config;
	}

	public boolean isEnabled(String code) {
		return ((this.getConfig().get(code) != null) 
				&& (this.getConfig().get(code).equalsIgnoreCase("true")));
	}


}
